package com.zhj.edu.iotserver.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.zhj.edu.constance.DeviceConstance;
import com.zhj.edu.exception.handler.BusinessException;
import com.zhj.edu.iotserver.mapper.DeviceTopicMapper;
import com.zhj.edu.pojo.Device;
import com.zhj.edu.iotserver.mapper.DeviceMapper;
import com.zhj.edu.iotserver.service.DeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhj.edu.pojo.DeviceTopic;
import com.zhj.edu.result.ResponseCode;
import com.zhj.edu.service.IJwtService;
import com.zhj.edu.utils.DeviceKeyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * <p>
 * 设备表 服务实现类
 * </p>
 *
 * @author xiaobobo
 * @since 2024-09-13
 */
@Service
@Slf4j
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {

    @Autowired
    private IJwtService jwtService;

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private DeviceTopicMapper deviceTopicMapper;

    @Override
    public void addDevice(Device device, HttpServletRequest req) {
        //校验参数合法性
        if (null==device || StringUtils.isEmpty(device.getDeviceTypeId()) || StringUtils.isEmpty(device.getName()) || StringUtils.isEmpty(device.getGroupId()) || StringUtils.isEmpty(device.getParentId()) || StringUtils.isEmpty(device.getProductId()) || null==device.getDeviceIndex() || null==device.getCount() || null==device.getPrice()){
            throw new BusinessException(ResponseCode.PARAMETER_EXCEPTION.getCode(), ResponseCode.PARAMETER_EXCEPTION.getMsg());
        }
        log.info("参数校验成功...");
        synchronized (this){
            //参数无误，生成设备key
            device.setDeviceKey(DeviceKeyUtils.getDeviceKey());
            //设置设备的密钥
            device.setDeviceSecret(UUID.randomUUID().toString().replace("-","").trim());
        }
        //设置租户id
        String token = req.getHeader("token");
        String userId = jwtService.getUserId(token);
        device.setTenantId(userId);
        //设置是否激活  1:未激活
        device.setActive(1);
        //将数据上传到数据库
        deviceMapper.insert(device);
        //下面还需要向设备主题中添加数据
        DeviceTopic deviceTopic = new DeviceTopic();
        //首先构建主题（APP-hardware）
        String topic1 = DeviceConstance.getAppControlHardwarePrefix()+device.getDeviceKey();
        deviceTopic.setTopic(topic1);
        deviceTopic.setDeviceKey(device.getDeviceKey());
        deviceTopicMapper.insert(deviceTopic);
        //接下来构建硬件向应用程序发送指令的主题
        DeviceTopic deviceTopic1 = new DeviceTopic();
        deviceTopic1.setDeviceKey(device.getDeviceKey());
        //构建主题
        String topic2 = DeviceConstance.getHardwareSendMessagePrefix()+device.getDeviceKey();
        deviceTopic1.setTopic(topic2);
        //插入数据
        deviceTopicMapper.insert(deviceTopic1);
        log.info("上传设备数据到数据库成功...");
    }
}
